<?php
include_once('config/config.php');
$auth = check_auth();
$action = $_GET['action'] ?? '';
include_once('./config/logout.php');
// 当前页
$page = $_GET['page'] ?? 1;

// 每页显示多少条数据
$limit = 3;

// 页码
$size = 5;

// 获取偏移量
$start = ($page - 1) * $limit;

// 获取数据
$jobSql = "SELECT `j`.`id` , `j`.`name` , `d`.`name` AS `dname` FROM `pre_job` AS `j` LEFT JOIN `pre_dep` AS `d` ON `j`.`depid` = `d`.`id` ORDER BY `j`.`id` DESC LIMIT $start,$limit";

// 获取数据总数
$countSql = "SELECT COUNT(*) FROM `pre_job` AS `j` LEFT JOIN `pre_dep` AS `d` ON `j`.`depid` = `d`.`id`";

$count = find($countSql);

// 查询
$jobData = finds($jobSql);
// var_dump($count);
// var_dump($jobData);
$showPage = showPage($page, $count['COUNT(*)'], $limit);
?>
<!DOCTYPE html>
<html lang="en">

<head>
	<?php include_once('common/meta.php'); ?>
</head>

<body>
	<?php include_once('common/header.php'); ?>
	<?php include_once('common/menu.php'); ?>
	<div class="content">
		<div class="header">
			<h1 class="page-title">职位列表</h1>
		</div>
		<ul class="breadcrumb">
			<li><a href="index.html">Home</a> <span class="divider">/</span></li>
			<li class="active">Index</li>
		</ul>

		<div class="container-fluid">
			<div class="row-fluid">
				<div class="btn-toolbar">
					<button class="btn btn-primary" onClick="location='jobAdd.php'"><i class="icon-plus"></i>添加职位</button>
				</div>
				<div class="well">
					<table class="table">
						<thead>
							<tr>
								<th>ID</th>
								<th>职位名称</th>
								<th>所属部门</th>
								<th style="width: 26px;"></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ($jobData as $item) { ?>
								<tr>
									<td><?php echo $item['id']; ?></td>
									<td><?php echo $item['name']; ?></td>
									<td><?php echo $item['dname']; ?></td>
									<td>
										<a href="jobEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
										<a href="#myModal" data-id="<?php echo $item['id']; ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
									</td>
								</tr>
							<?php } ?>
						</tbody>
					</table>
				</div>
				<div class="pagination">
					<ul>
						<?php echo $showPage; ?>
					</ul>
				</div>

				<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						<h3 id="myModalLabel">删除职位</h3>
					</div>
					<div class="modal-body">
						<p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除职位？</p>
					</div>
					<div class="modal-footer">
						<button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
						<button class="btn btn-danger delete" data-dismiss="modal">删除</button>
					</div>
				</div>

				<?php include_once('common/footer.php') ?>

			</div>
		</div>
	</div>
</body>

</html>
<?php include_once('common/script.php') ?>
<script>
        // 全局职位id
        let id = null;

$('a[href="#myModal"]').click(function(){
    // 获取类似data-dismiss以data为前缀的值
    id = $(this).data('id');// js dateset
});

$('.delete').click(function(){
    $.ajax({
        type:'post',
        url:'api.php',
        data:{
            id,
            action:'job_del'
        },
        dataType:'json',
        success:function(res)
        {
            if(res && res.code === 1)
            {
                alert(res.msg);
                location.reload();
                return;
            }else{
                alert(res.msg);
                return;
            }
        }
    })
});

</script>